home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / wnos / wn941101 / ttydriv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-10  |  6.0 KB  |  275 lines

  1. /* TTY input driver */
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <conio.h>
  5. #include "global.h"
  6. #include "mbuf.h"
  7. #include "session.h"
  8. #include "tty.h"
  9. #include "socket.h"
  10.  
  11. #define    OFF            0
  12. #define    ON            1
  13. #define    CTLB        02        /* use as F3 in dos but no editing */
  14. #define CTLK         0x0b    /* previous session             */
  15. #define CTLL         0x0c    /* next session                 */
  16. #define CTLO         0x0f    /* previous Cstack              */
  17. #define CTLP         0x10    /* next Cstack                  */
  18. #define    CTLU        21        /* delete current line in total */
  19. #define    CTLW        23        /* erase last word including preceding space */
  20. #define    CTLZ        26        /* EOF char in dos */
  21.  
  22. #define DEL            0x7f
  23. #define CLINELEN    162
  24.  
  25. static int  Lastsize = 1;
  26. static char Lastline[CLINELEN + 1] = "\n";
  27.  
  28. static void near
  29. sets(struct session *sp)
  30. {
  31.     sp->tsavex = wherex();
  32.     sp->tsavey = wherey();
  33.     window(1,Nrows-1,80,Nrows);
  34.     gotoxy(sp->bsavex,sp->bsavey);
  35. }
  36.  
  37. static void near
  38. resets(struct session *sp)
  39. {
  40.    sp->bsavex = wherex();
  41.    sp->bsavey = wherey();
  42.    window(1,3,80,Nrows-2);
  43.    gotoxy(sp->tsavex,sp->tsavey);
  44. }
  45.  
  46. /* Accept characters from the incoming tty buffer and process them
  47.  * (if in cooked mode) or just pass them directly (if in raw mode).
  48.  *
  49.  * Echoing (if enabled) is direct to the raw terminal. This requires
  50.  * recording (if enabled) of locally typed info to be done by the session
  51.  * itself so that edited output instead of raw input is recorded.
  52.  * Control-W added by g1emm again.... for word delete.
  53.  * Control-B/Function key 3 added by g1emm for line repeat.
  54.  */
  55. struct mbuf *
  56. ttydriv(struct session *sp,char c)
  57. {
  58.     struct mbuf *bp;
  59.     char *cp, *rp;
  60.     int cnt;
  61.  
  62.     switch(sp->ttystate.edit){
  63.     case OFF:
  64.         bp = ambufw(1);
  65.         *bp->data = c;
  66.         bp->cnt = 1;
  67. #ifndef AMIGA
  68.         if(c != CTLZ && sp->ttystate.echo) {
  69. #else
  70.         if(sp->ttystate.echo) {
  71. #endif
  72.             if(sp->split) {
  73.                 sets(sp);
  74.                 putch(c);
  75.                 cputs("_\b");
  76.                 resets(sp);
  77.             } else {
  78.                 putch(c);
  79.             }
  80.         }
  81.         return bp;
  82.     case ON:
  83.         if(sp->ttystate.line == NULLBUF)
  84.             sp->ttystate.line = ambufw(LINESIZE);
  85.  
  86.         bp = sp->ttystate.line;
  87.         cp = bp->data + bp->cnt;
  88.         /* Perform cooked-mode line editing */
  89.         switch(c) {
  90.         case '\r':    /* CR and LF both terminate the line */
  91.         case '\n':
  92.             *cp = (sp->ttystate.crnl) ? '\n' : c;
  93.             if(sp->ttystate.echo) {
  94.                 if(sp->split) {
  95.                     rp = bp->data;
  96.                     while(rp < cp) {
  97.                         putch(*rp++);
  98.                     }
  99.                     if(sp != Trace)
  100.                         cputs(Eol);
  101.                     else
  102.                         if(bp->cnt)
  103.                             cputs(Eol);
  104.                     sets(sp);
  105.                     clrscr();
  106.                     cputs("_\b");
  107.                     resets(sp);
  108.                 } else {
  109.                     if (Current != Command)
  110.                         cputs(Eol);
  111.                     else if (bp->cnt) {
  112.                         cputs(Eol);
  113.                         Command->flag = PPROMPT;
  114.                     }
  115.                 }
  116.             }
  117.             bp->cnt++;
  118.             sp->ttystate.line = NULLBUF;
  119.             Lastsize = bp->cnt;
  120.  
  121.             /* Commandstack by DK5DC*/
  122.             if(bp->cnt > 1) {
  123.                 memcpy(Lastline, bp->data, Lastsize);
  124.                 memcpy(sp->Cs[sp->cont], bp->data, Lastsize);
  125.                 *(sp->Cs[sp->cont] + Lastsize) = '\0';
  126.                 if (++sp->cont == MAXCS)
  127.                     sp->cont = 0;
  128.             }
  129.             /* End Commandstack by Glasi*/
  130.             return bp;
  131.         case '\b':        /* Backsp->ttystateace */
  132.             if(bp->cnt > 0){
  133.                 bp->cnt--;
  134.                 if(sp->ttystate.echo) {
  135.                     if(sp->split) {
  136.                         sets(sp);
  137.                         cputs(" \b\b_\b");
  138.                         resets(sp);
  139.                     } else {
  140.                         cputs("\b \b");
  141.                     }
  142.                 }
  143.             }
  144.             break;
  145.         case CTLU:    /* Line kill */
  146.             if(sp->split) {
  147.                 sets(sp);
  148.                 clrscr();
  149.                 bp->cnt = 0;
  150.                 gotoxy(1,1);
  151.                 cputs("_\b");
  152.                 resets(sp);
  153.             } else {
  154.                 while(bp->cnt > 0){
  155.                     bp->cnt--;
  156.                     if(sp->ttystate.echo)
  157.                         cputs("\b \b");
  158.                 }
  159.             }
  160.             break;
  161.         case CTLB:    /* Use last line to finish current */
  162.             cnt = bp->cnt;        /* save count so far */
  163.             if(sp->split)
  164.                 sets(sp);
  165.             while(bp->cnt > 0) {
  166.                 if(sp->ttystate.echo)
  167.                     cputs("\b \b");
  168.                 bp->cnt--;
  169.             }
  170.             if(sp->split)
  171.                 resets(sp);
  172.             bp->cnt = cnt;
  173. pstr:
  174.             if(bp->cnt < (Lastsize-1)){
  175.                 memcpy(bp->data+bp->cnt, &Lastline[bp->cnt], (Lastsize-1) - bp->cnt);
  176.                 bp->cnt = Lastsize-1;
  177.             }
  178.             *(bp->data + bp->cnt) = '\0';    /* make it a string */
  179.             if(sp->ttystate.echo) {
  180.                 if(sp->split)
  181.                     sets(sp);
  182.                 cputs(bp->data);
  183.                 clreol();
  184.                 if(sp->split) {
  185.                     cputs("_\b");
  186.                     resets(sp);
  187.                 }
  188.             }
  189.             break ;
  190.         case CTLP:
  191.             ++sp->cont;
  192.             if (sp->cont >= MAXCS || sp->Cs[sp->cont][0] == '\0')
  193.                 sp->cont = 0;
  194. docs:
  195.             while(bp->cnt > 0){
  196.                 bp->cnt--;
  197.                 if(sp->ttystate.echo){
  198.                     if(sp->split)
  199.                         sets(sp);
  200.                     cputs("\b \b");
  201.                     if(sp->split)
  202.                         resets(sp);
  203.                 }
  204.             }
  205.             if (sp->Cs[sp->cont][0] != '\0') {
  206.                 strcpy(Lastline,sp->Cs[sp->cont]);
  207.                 Lastsize = strlen(Lastline);
  208.                 bp->cnt = 0;
  209.             }
  210.             goto pstr;
  211.         case CTLO:
  212.             if (sp->Cs[0][0] != '\0') {
  213.                 if (sp->cont > 0)
  214.                     --sp->cont;
  215.                 else
  216.                     sp->cont = MAXCS-1;
  217.  
  218.                 while(sp->Cs[sp->cont][0] == '\0')
  219.                     --sp->cont;
  220.             }
  221.             goto docs;
  222.         case CTLW:    /* erase word */
  223.             cnt = 0 ;    /* we haven't seen a printable char yet */
  224.             while(bp->cnt > 0){
  225.                 *(bp->data + bp->cnt--) = '\n';
  226.                 if(sp->ttystate.echo) {
  227.                     if(sp->split) {
  228.                         sets(sp);
  229.                         cputs(" \b\b_\b");
  230.                         resets(sp);
  231.                     } else {
  232.                         cputs("\b \b");
  233.                     }
  234.                 }
  235.                 if (isspace((int)*(bp->data + bp->cnt))) {
  236.                     if (cnt)
  237.                         break ;
  238.                 } else {
  239.                     cnt = 1 ;
  240.                 }
  241.             }
  242.             break ;
  243.         default:    /* Ordinary character */
  244.             *cp = c;
  245.             bp->cnt++;
  246.  
  247.             /* ^Z apparently hangs the terminal emulators under
  248.              * DoubleDos and Desqview. I REALLY HATE having to patch
  249.              * around other people's bugs like this!!!
  250.              */
  251.             if(bp->cnt < CLINELEN - 4 &&
  252. #ifndef    AMIGA
  253.                    c != CTLZ && sp->ttystate.echo) {
  254. #else
  255.                    sp->ttystate.echo) {
  256. #endif
  257.                 if(sp->split) {
  258.                     sets(sp);
  259.                     putch(c);
  260.                     cputs("_\b");
  261.                     resets(sp);
  262.                 } else
  263.                     putch(c);
  264.             } else if(bp->cnt >= CLINELEN - 4) {
  265.                 putch('\007');                    /* Beep */
  266.                 bp->cnt--;
  267.             }
  268.             break;
  269.         }
  270.         break;
  271.     }
  272.     return NULLBUF;
  273. }
  274.  
  275.